package net.wanho.listerner;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import net.wanho.entity.User;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * Author：汤小洋
 * Date：2025-07-29 16:46
 * Description：<描述>
 */
@Component
public class MsgListener {

    @RabbitListener(queues = "${shop.queue}")
    // public void readMsg(String msg){
    public void readMsg(User user){
        System.out.println("==============>" + user);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("queue2"),
            exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void readMsg2(String msg){
        System.out.println("queue2========>" + msg);
    }

    /**
     * 消息可靠性
     */
    @SneakyThrows
    @RabbitListener(queues = "queue1")
    public void readMsg(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){
        System.out.println("==============>" + msg);

        // todo  业务处理

        // 确认消息,处理消息成功
        // channel.basicAck(tag, false); // 消息标签 / 是否批量处理

        // 未确认消息,处理消息失败
        channel.basicNack(tag, false, false); // 消息标签 / 是否批量处理 / 是否重新入队

        /**
         * 注意:如果未确认消息,且不重新入队,则消息会被丢弃,变成死信!!
         */


    }
}
